#include "vmlinux.h"
#include "unrandomize.bpf.h"

#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>

struct s_filter filter = {
	.pid = -1,
	.uid = -1,
};

struct {
	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
	__uint(key_size, sizeof(u32));
	__uint(value_size, sizeof(u32));
}events SEC(".maps");

SEC("kprobe/arch_mmap_rnd")
int kprobe_arch_mmap_rnd(void *ctx) {
    int pid = bpf_get_current_pid_tgid() >> 32;
    int uid = bpf_get_current_uid_gid() & 0xffffffff;

    if (filter.pid != -1 && filter.pid != pid)
        return 0;
    if (filter.uid != -1 && filter.uid != uid)
        return 0;

    bpf_override_return(ctx, 0);

    struct s_event event = {};
    event.pid = pid;
    event.uid = uid;
    bpf_get_current_comm(&event.comm, sizeof(event.comm));
    bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &event, sizeof(event));

    return 0;
}

char _license[] SEC("license") = "GPL";